#include "DiagonalStair.h"
#include "Step.h"
#include "AnimationFilmHolder.h"
#include "Timer.h"

void DiagonalStair::AnimateUp(Sprite *s)  {
	if(animator->HasFinished()) {
		//s->SetAnimationFilm((AnimationFilm *)AnimationFilmHolder::GetSingleton()->GetFilm("popeye_climbup_diagonal"));
		s->DisableMovement(); //player should not be allowed to move during the animation
		animator->SetOnFinish(UpStepCompleted, new AnimatorData(s, this));
		animator->Start(s, up, Timer::getgametime());
		AnimatorHolder::MarkAsRunning(animator);
	}
};

void DiagonalStair::AnimateDown(Sprite *s)  {
	if(animator->HasFinished()) {
		//s->SetAnimationFilm((AnimationFilm *)AnimationFilmHolder::GetSingleton()->GetFilm("popeye_climbdown_diagonal"));
		s->DisableMovement(); //player should not be allowed to move during the animation
		animator->SetOnFinish(DownStepCompleted, new AnimatorData(s, this));
		animator->Start(s, down, Timer::getgametime());
		AnimatorHolder::MarkAsRunning(animator);
	}
};

void DiagonalStair::UpStepCompleted (Animator* _animator, void* closure) {
	//1. Ti ein' touto to Animator* xwris onoma metavlhths sth me8odo? isxyei kati tetoio?!?!
	//	Ayto ienai apla gia tis dhlwseis, mporeis na kaneis skio to onoma, sto implementation prepei na baleis kati
	//2. Prepei na mpei ena GetStep() kai SetStep sto Sprite (??)
	// yep, done
	//3. Prepei na mpei ena SetPlatform() sto sprite (??)
	//yep, done
	AnimatorData* data = (AnimatorData*) closure;
	Sprite* player = data->first;
	Stair* stair = data->second;
	
	if (Step* step = (Step*)player->GetStep()) { //player was already on the stair
		Step* up = stair->GetUpStep(step);
		assert(up);

		if (up == stair->GetTopStep()) {
			up = (Step *) 0; //not on the stair anymore, but on the platform
			player->SetPlatform(stair->GetTopPlatform());
			player->EnableMovement();
			player->DisableClimbing();
			player->ResetToDefaultFilm();
		}

		player->SetStep(up);
	}
	else //player was on bottom platform
		player->SetStep(stair->GetBottomStep());
		
	AnimatorHolder::DelayedSuspention(_animator);
	//delete data;
}

void DiagonalStair::DownStepCompleted (Animator* _animator, void* closure) {
	AnimatorData* data = (AnimatorData*) closure;
	Sprite* player = data->first;
	Stair* stair = data->second;
		
	if (Step* step = (Step*)player->GetStep()) { //player was already on the stair
		Step* down = stair->GetDownStep(step);
		//assert(down);

		if (down == NULL) {
			down = (Step *) 0; //not on the stair anymore, but on the platform
			player->SetPlatform(stair->GetBottomPlatform());
			player->EnableMovement();
			player->DisableClimbing();
			player->ResetToDefaultFilm();
		}

		player->SetStep(down);
	}
	else {//player was on bottom platform
		Step* down = stair->GetDownStep(stair->GetTopStep());
		player->SetStep(down);
	}
		
	AnimatorHolder::DelayedSuspention(_animator);
	//delete data;
}
